<?php
	
if ((isset($_SESSION['identifiant'])) AND (isset($_SESSION['groupe'])==2))
{
	if (isset($_POST['modifier'])) 
	{
		$req = $bdd->prepare('UPDATE groupes SET nom = :nom, description = :description WHERE id=:id');
		$req->execute(array(
			'nom' => $_POST['nom'],
			'description' => $_POST['description'],
			'id' => $_POST['id']
		));
		header('Location: page.php?page=admin_groupes'); 
	}
	elseif (isset($_POST['effacer'])) 
	{
		$req = $bdd->prepare('DELETE FROM groupes WHERE id=:id');
		$req->execute(array(
				'id' => $_POST['id']
		));
		header('Location: page.php?page=admin_groupes'); 
	}
	elseif (isset($_POST['ajouter'])) 
	{
		$req = $bdd->prepare('INSERT INTO groupes (nom, description) VALUES (:nom, :description)');
		$req->execute(array(
			'nom' => $_POST['nom'],
			'description' => $_POST['description']
		));
	}
	
	?>
	<h1 id="titre">Gestion des groupes</h1>
	<table>
	<tr><th>Statut</th><th>Explication</th><th></th></tr>
	<tr><td colspan="3"><hr /></td></tr>
	<?php
	$req = $bdd->query('SELECT * FROM groupes WHERE id <= 3');
	while ($donnees = $req->fetch())
	{
		?>
		<tr><td><?php echo $donnees['nom']; ?></td>	<td><?php echo $donnees['description']; ?></td><td width="25%"></td></tr>
		<?php
	}
	$req->closeCursor();
	
	$req = $bdd->query('SELECT * FROM groupes WHERE id > 3');
	?>
	<tr><td colspan="3"><hr /></td></tr>
		<?php
		while ($donnees = $req->fetch())
		{
			?>
			<form method="post" action="page.php?page=admin_groupes">
				
				<tr>
				<td><input type="text" name="nom" value="<?php echo $donnees['nom']; ?>" maxlength="250" size="15"></td>
				<td><input type="text" name="description" value="<?php echo $donnees['description']; ?>" maxlength="250" size="40"><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" /></td>
				<td><input type="submit" value="Modifier" name="modifier" /><input type="submit" value="Effacer" name="effacer" /></td>
				</tr>
			</form>
			<?php
		}
		$req->closeCursor();
		?>
		<tr><td colspan="3"><hr /></td></tr>
		<form method="post" action="page.php?page=admin_groupes">
			<tr>
			<td><input type="text" name="nom" value="" maxlength="250" size="15"></td>
			<td><input type="text" name="description" maxlength="250" size="40"></td>
			<td width="25%"><input type="submit" value="Ajouter ce statut" name="ajouter" /></td>
			</tr>
		</form>
		</table>
	<?php
}
else
{
	echo NON_ACCES_ADMIN;
}
?>